package com.example.delll.kugou.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.delll.kugou.R;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SYFragment extends Fragment {

    private static final String TAG = "MainActivity";

    private TabLayout tabLayout;
    private ViewPager viewPaper;
    private List<String> titleList;

    public SYFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sy, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayout = getView().findViewById(R.id.tab_layout);
        viewPaper = getView().findViewById(R.id.vp_show);
        titleList = new ArrayList<>();
        titleList.add("关注");
        titleList.add("头条");
        titleList.add("视频");
        titleList.add("娱乐");
        SYFragmentViewPaper syFragmentViewPaper = new SYFragmentViewPaper(getChildFragmentManager());
        viewPaper.setAdapter(syFragmentViewPaper);
        tabLayout.setupWithViewPager(viewPaper);
        viewPaper.setCurrentItem(1);
    }

    class SYFragmentViewPaper extends FragmentPagerAdapter{

        public SYFragmentViewPaper(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public Fragment getItem(int position) {
//            Log.d(TAG, "getItem: "+position);
            Bundle bundle = new Bundle();
            switch (position) {
                case 0:
//                    bundle.clear();
                    GuanZuFragment guanZuFragment = new GuanZuFragment();
//                    bundle.putInt("index",position);
//                    guanZuFragment.setArguments(bundle);
                    return guanZuFragment;
                case 1:
//                    bundle.clear();
                    TouTiaoFragment touTiaoFragment = new TouTiaoFragment();
                    bundle.putInt("index",position);
                    touTiaoFragment.setArguments(bundle);
                    return touTiaoFragment;
                case 2:
//                    bundle.clear();
                    ShiPingFragment shiPingFragment = new ShiPingFragment();
//                    bundle.putInt("index",position);
//                    shiPingFragment.setArguments(bundle);
                    return shiPingFragment;
                case 3:
//                    bundle.clear();
                    YuLeFragment yuLeFragment = new YuLeFragment();
//                    bundle.putInt("index",position);
//                    yuLeFragment.setArguments(bundle);
                    return yuLeFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return titleList.size();
        }
    }
}
